import "dart:developer";

import 'package:ball_lion/entity/record/team_entity.dart';

import 'package:ball_lion/entity/record/basketball/lq_channel_entity.dart';
import 'package:ball_lion/entity/record/player_entity.dart';
import 'package:ball_lion/entity/record/category_list_entity.dart';
import 'package:ball_lion/entity/record/points_entity.dart';
import 'package:ball_lion/entity/record/rank_entity.dart';
import 'package:ball_lion/entity/record/channel_entity.dart';
import 'package:ball_lion/entity/record/football/zq_schedule_entity.dart';
import 'package:ball_lion/entity/record/football/zq_transfer_entity.dart';
import 'package:ball_lion/entity/record/recent_channel_entity.dart';
import 'package:ball_lion/utils/user.dart';
import '../api_dir.dart';
import '../http/dio_utils.dart';

class RecordApi {
  static Future<List<ChannelEntity>?> myChannels(int index) async {
    try {
      final r = await DioUtils.post(
          index == 0
              ? "/${ApiDir.data}/app-do/leagueChannel"
              : "/${ApiDir.basketball}/app-basketball-data-do/leagueBbChannelList",
          params: {"userId": User.auth?.userId});
      final l = r.data["d"] as List;
      return l.map((e) => ChannelEntity.fromJson(e)).toList();
    } catch (err) {
      log("my  channels err $err");
    }
    return null;
  }

  static Future<List<ChannelAreaEntity>?> channelsAll(int index) async {
    try {
      final r = await DioUtils.post(index == 0
          ? "/${ApiDir.data}/app-do/leagueChannelAll"
          : "/${ApiDir.basketball}/app-basketball-data-do/leagueBbChannelAll");
      final l = r.data["d"] as List;
      return l.map((e) => ChannelAreaEntity.fromJson(e)).toList();
    } catch (err) {
      log("$err");
    }
    return null;
  }

  static Future<bool> updateChannel(List<ChannelEntity> l, int index) async {
    try {
      final r = await DioUtils.post(
          index == 0
              ? "/${ApiDir.data}/app-do/leagueChannelUpdate"
              : "/${ApiDir.basketball}/app-basketball-data-do/leagueBbChannelUpdate",
          params: {
            "leagueIds": l.map((e) => e.channelId).toList(),
            "userId": User.auth?.userId
          });
      return r.data["c"] == 200;
    } catch (err) {}
    return false;
  }

  static Future<List<RecentChannelEntity>> recentChannel(int sportsId) async {
    try {
      final r = await DioUtils.post("/data/app-do/todayLeague",
          params: {"sportsId": sportsId});
      final l = r.data['d'] as List;
      return l.map((e) => RecentChannelEntity.fromJson(e)).toList();
    } catch (err) {
      return [];
    }
  }

  ///联赛查询
  static Future<ChannelEntity?> getChannel(String leagueId, int index) async {
    try {
      final r = await DioUtils.post(
          index == 0
              ? "/${ApiDir.data}/app-do/leagueChannelById"
              : "/${ApiDir.basketball}/app-basketball-data-do/leagueBbChannelById",
          params: {"leagueId": leagueId});
      final l = r.data["d"] as List;
      if (l.isNotEmpty) {
        return ChannelEntity.fromJson(l[0]);
      }
    } catch (err) {
      log(" err $err");
    }
    return null;
  }

  ///球员球队数据选项列表
  static Future<List<CategoryListEntity>?> getCategory(
      {String? season,
      int? seasonId,
      required int leagueId,
      bool isTeam = false,
      required int recordType}) async {
    final params = {
      "qxbLeagueId": leagueId,
      if (recordType == 0) "season": season,
      if (recordType == 1) "seasonId": seasonId
    };
    final result = await DioUtils.post(
        "${recordType == 0 ? "/${ApiDir.data}/app-channel-do/" : "/${ApiDir.basketball}/app-basketball-data-do/"}"
        "${isTeam ? "teamStruct" : "playerStruct"}",
        params: params);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List<CategoryListEntity> data = result.data["d"]["searchTypes"]
          .map<CategoryListEntity>((e) => CategoryListEntity.fromJson(e))
          .toList();
      return data;
    }
    return null;
  }

  ///球队
  static Future<AllTeamEntity?> getTeamData(
      String? season, int qxbLeagueId, int searchType, int type, int seasonId,
      {bool needLoading = false}) async {
    final params = {
      "qxbLeagueId": qxbLeagueId,
      if (type == 0) "season": season,
      if (type == 1) "seasonId": seasonId,
      "searchType": searchType
    };
    final result = await DioUtils.post(
        type == 0
            ? "/${ApiDir.data}/app-channel-do/team"
            : "/${ApiDir.basketball}/app-basketball-data-do/team",
        params: params);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      AllTeamEntity data = AllTeamEntity.fromJson(result.data["d"]);
      return data;
    }
    return null;
  }

  ///球员
  static Future<AllPlayerEntity?> getPlayerData(
      {String? season,
      int? seasonId,
      required int leagueId,
      required int searchType,
      required int recordType}) async {
    final params = {
      "qxbLeagueId": leagueId,
      if (recordType == 0) "season": season,
      if (recordType == 1) "seasonId": seasonId,
      "searchType": searchType
    };
    final result = await DioUtils.post(
        recordType == 0
            ? "/${ApiDir.data}/app-channel-do/player"
            : "/${ApiDir.basketball}/app-basketball-data-do/player",
        params: params);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      AllPlayerEntity data = AllPlayerEntity.fromJson(result.data["d"]);
      return data;
    }
    return null;
  }

  ///数据赛程
  static Future<List<ScheduleEntity>> getSchedule(
      {int? leagueId, String? season, int? seasonId, int? recordType}) async {
    try {
      final r = await DioUtils.post(
          recordType == 0
              ? "/${ApiDir.data}/app-do/schedule"
              : "/${ApiDir.basketball}/app-basketball-data-do/schedule",
          params: {
            "leagueId": leagueId,
            if (recordType == 0) "season": season,
            if (recordType == 1) "seasonId": seasonId
          });
      final l = r.data["d"] as List;
      var data = l
          .map((e) => recordType == 0
              ? ScheduleEntity.fromSoccer(e)
              : ScheduleEntity.fromBasketball(e))
          .toList();
      return data;
    } catch (err) {
      return [];
    }
  }

  ///数据积分
  static Future<PointsEntity?> getPoints(
      {int? leagueId, String? season, int? seasonId, int? recordType}) async {
    final params = {
      if (recordType == 0) "qxbLeagueId": leagueId,
      if (recordType == 0) "season": season,
      if (recordType == 1) "leagueId": leagueId,
      if (recordType == 1) "seasonId": seasonId
    };
    final result = await DioUtils.post(
        recordType == 0
            ? "/${ApiDir.data}/app-channel-do/cupStanding"
            : "/${ApiDir.basketball}/app-basketball-data-do/rank",
        params: params);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      PointsEntity? data = recordType == 0
          ? PointsEntity.fromSoccer(result.data["d"])
          : PointsEntity.fromBasketball(result.data["d"]);
      return data;
    }
    return null;
  }

  ///数据转会
  static Future<TransferEntity?> getZqTransfer(
      int qxbLeagueId, String season) async {
    final params = {
      "qxbLeagueId": qxbLeagueId,
      "season": season,
    };
    final result = await DioUtils.post(
        "/${ApiDir.data}/app-channel-do/transfer",
        params: params);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      TransferEntity data = TransferEntity.fromJson(result.data["d"]);
      return data;
    }
    return null;
  }

  ///篮球数据频道
  static Future<List<LqChannelEntity>?> getLqChannel(int leagueId) async {
    try {
      final r = await DioUtils.post(
          "/${ApiDir.basketball}/app-basketball-data-do/getChannelId",
          params: {"leagueId": leagueId});
      final l = r.data["d"] as List;
      return l.map((e) => LqChannelEntity.fromJson(e)).toList();
    } catch (err) {}
    return null;
  }

  ///数据排名
  static Future<List<ZqRankEntity>> getZqRank(
      {required int type, String? area, String? month}) async {
    final params = {
      "type": type, // 1男足 2女足 3俱乐部
      "rankMonth": month,
      "areaCn": area
    };
    final result =
        await DioUtils.post("/${ApiDir.data}/app-do/rankList", params: params);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List<ZqRankEntity> data = result.data["d"]
          .map<ZqRankEntity>((e) => ZqRankEntity.fromJson(e))
          .toList();
      return data;
    }
    return [];
  }

  ///数据排名月份
  static Future<List<ZqRankMonthEntity>> getZqRankMonth(int type) async {
    final result = await DioUtils.post("/${ApiDir.data}/app-do/rankMonthList",
        params: {"type": type});
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List<ZqRankMonthEntity> data = result.data["d"]
          .map<ZqRankMonthEntity>((e) => ZqRankMonthEntity.fromJson(e))
          .toList();
      return data;
    }
    return [];
  }

  ///篮球数据排名
  static Future<List<LqRankEntity>> getLqRank(
      {required int type, int? area, String? month}) async {
    final params = {
      "type": type, // 1男篮 2女篮
      "month": month,
      "regionId": area
    };
    final result = await DioUtils.post(
        "/${ApiDir.basketball}/app-basketball-data-do/fibaRank",
        params: params);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List<LqRankEntity> data = result.data["d"]
          .map<LqRankEntity>((e) => LqRankEntity.fromJson(e))
          .toList();
      return data;
    }
    return [];
  }

  ///篮球数据排名月份
  static Future<List<LqRankMonthEntity>> getLqRankMonth(int type) async {
    final result = await DioUtils.post(
        "/${ApiDir.basketball}/app-basketball-data-do/fibaRankMonth",
        params: {"type": type});
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List<LqRankMonthEntity> data = result.data["d"]
          .map<LqRankMonthEntity>((e) => LqRankMonthEntity.fromJson(e))
          .toList();
      return data;
    }
    return [];
  }
}
